package StackDemo;

/**
 * 用链表模拟栈（stack）
 */
public class LinkedsSimulationStack {
    public static void main(String[] args) {
        People p1 = new People(1,"sun");
        People p2 = new People(2,"jia");
        People p3 = new People(3,"fun");
        SimulationStack s = new SimulationStack();

        s.push(p1);
        s.push(p2);
       People pe =  s.pop();
        System.out.println("取出的数据"+pe);
        s.push(p3);
        s.show();

    }
}
class SimulationStack{
    //头队列
    People head = new People(0,"0");
    public void push(People p){//添加
        People temp = head;
        while(true){
            if(temp.next==null){
                temp.next=p;
                p.last=temp;
                return;
            }
            temp=temp.next;
        }
    }
    public People pop(){
        People temp = head.next;
        while(true){
            if(temp.next==null){
                temp.last.next=null;
                return temp;
            }
            temp=temp.next;
        }
    }
    public void show(){
        People temp = head.next;
        while(true){
            if(temp==null){
                return;
            }
            System.out.println(temp);
            temp=temp.next;
        }
    }

}
class People{
    People last;
    People next;
    int no;
    String name;
    People(int no,String name){
        this.no=no;
        this.name=name;
    }
    public String toString() {
        return "People{" + ", name='" + name + '\'' + "no="+no+'}';
    }
}